/*
* 修改账单数据库
* 新加字段dateTime 为录入数据当天0点的时间戳
* */
const mongoose = require('mongoose');
// 连接数据库
const con = require('../libs/DBConect');
// 引入对应数据库文件
const db = require('../dbs/voiceAccountBook');

async function update() {
  console.log('开始更新数据');
  const data = await db.query({
    queryData: {}, options: {
      __v: 0,
      _id: 0,
      money: 0,
      desc: 0,
      createDate: 0,
      updateDate: 0,
      user: 0,
      payType: 0, // 付款类型
      consumptionType: 0
    }
  });
  let length = data.length;
  console.log(length);
  for (let i = 0; i < length; i++) {
    let item = data[i];
    let date = '';
    let dateTime = 0;
    if (item.year && item.month && item.date) {
      dateTime = new Date(item.year, item.month - 1, item.date).getTime();
      date = `${item.year}-${compute(item.month)}-${compute(item.date)}`;
    }
    console.log('更新数据', item.id, date);
    await db.update({
      queryData: {
        id: item.id
      },
      data: {
        $set: {
          dateTime,
          date
        }
      }
    });
  }
  console.log('更新数据完成');
  mongoose.disconnect();
}

function compute(number) {
  return number > 9 ? number.toString() : '0' + number;
}

setTimeout(update, 500);
